*for REStat paper, "Breastfeeding and Children's Early Cognitive Outcomes"


libname dd 'X:\Donnanew';

data x;
  set dd.momdadid;

proc sort;  by dadid;  run;

data y;
set dd.pind08;

dadid = er30001*1000 + er30002;

proc sort;  by dadid;

data z;
merge x y;  by dadid;  


if childid ne .;

data dd.daded08; 
set z;
keep  childid dadhgc dhgc97;

dadhgc = .;

if childyob = 1983 then do;
   if er30413 gt 0 and er30413 lt 18 then dadhgc = er30413;
end;
if childyob = 1984 then do;
   if (er30433 gt 0 and er30433 lt 18) then dadhgc = er30433;
   else if (er30413 gt 0 and er30413 lt 18) then dadhgc = er30413;
   else if (er30478 gt 0 and er30478 lt 18) then dadhgc = er30478;
   else if (er30384 gt 0 and er30384 lt 18) then dadhgc = er30384;
   else if (er30513 gt 0 and er30513 lt 18) then dadhgc = er30513;
end;

if childyob = 1985 then do;
   if (er30478 gt 0 and er30478 lt 18) then dadhgc = er30478;
   else if (er30433 gt 0 and er30433 lt 18) then dadhgc = er30433;
   else if (er30513 gt 0 and er30513 lt 18) then dadhgc = er30513;
   else if (er30413 gt 0 and er30413 lt 18) then dadhgc = er30413;
   else if (er30549 gt 0 and er30549 lt 18) then dadhgc = er30549;
end;

if childyob = 1986 then do;
   if (er30513 gt 0 and er30513 lt 18) then dadhgc = er30513;
   else if (er30478 gt 0 and er30478 lt 18) then dadhgc = er30478;
   else if (er30549 gt 0 and er30549 lt 18) then dadhgc = er30549;
   else if (er30433 gt 0 and er30433 lt 18) then dadhgc = er30433;
   else if (er30584 gt 0 and er30584 lt 18) then dadhgc = er30584;
 end;

if childyob = 1987 then do;
   if (er30549 gt 0 and er30549 lt 18) then dadhgc = er30549;
   else if (er30513 gt 0 and er30513 lt 18) then dadhgc = er30513;
   else if (er30584 gt 0 and er30584 lt 18) then dadhgc = er30584;
   else if (er30478 gt 0 and er30478 lt 18) then dadhgc = er30478;
   else if (er30620 gt 0 and er30620 lt 18) then dadhgc = er30620;
    end;

if childyob = 1988 then do;
   if (er30584 gt 0 and er30584 lt 18) then dadhgc = er30584;
   else if (er30549 gt 0 and er30549 lt 18) then dadhgc = er30549;
   else if (er30620 gt 0 and er30620 lt 18) then dadhgc = er30620;
   else if (er30513 gt 0 and er30513 lt 18) then dadhgc = er30513;
   else if (er30657 gt 0 and er30657 lt 18) then dadhgc = er30657;
     end;

  if childyob = 1989 then do;
   if (er30620 gt 0 and er30620 lt 18) then dadhgc = er30620;
   else if (er30584 gt 0 and er30584 lt 18) then dadhgc = er30584;
   else if (er30657 gt 0 and er30657 lt 18) then dadhgc = er30657;
   else if (er30549 gt 0 and er30549 lt 18) then dadhgc = er30549;
   else if (er30703 gt 0 and er30703 lt 18) then dadhgc = er30703;
       end;

  if childyob = 1990 then do;
  if (er30657 gt 0 and er30657 lt 18) then dadhgc = er30657;
  else if (er30620 gt 0 and er30620 lt 18) then dadhgc = er30620;
  else if (er30703 gt 0 and er30703 lt 18) then dadhgc = er30703;
  else if (er30584 gt 0 and er30584 lt 18) then dadhgc = er30584;
  else if (er30748 gt 0 and er30748 lt 18) then dadhgc = er30748;
       end;

  if childyob = 1991 then do;
  if (er30703 gt 0 and er30703 lt 18) then dadhgc = er30703;
  else if (er30657 gt 0 and er30657 lt 18) then dadhgc = er30657;
  else if (er30748 gt 0 and er30748 lt 18) then dadhgc = er30748;
  else if (er30620 gt 0 and er30620 lt 18) then dadhgc = er30620;
  else if (er30820 gt 0 and er30820 lt 18) then dadhgc = er30820;
*fill in a few more;
   if (dadhgc = . and er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
    end;

if childyob = 1992 then do;
  if (er30748 gt 0 and er30748 lt 18) then dadhgc = er30748;
  else if (er30703 gt 0 and er30703 lt 18) then dadhgc = er30703;
  else if (er30820 gt 0 and er30820 lt 18) then dadhgc = er30820;
  else if (er30657 gt 0 and er30657 lt 18) then dadhgc = er30657;
  else if (er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
  *fill in a few more;
  if (dadhgc = . and er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
end;

if childyob = 1993 then do;
  if (er30820 gt 0 and er30820 lt 18) then dadhgc = er30820;
  else if (er30748 gt 0 and er30748 lt 18) then dadhgc = er30748;
  else if (er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
  else if (er30703 gt 0 and er30703 lt 18) then dadhgc = er30703;
  else if (er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
  *fill in a few more;
  if (dadhgc = . and er33315 gt 0 and er33315 lt 18) then dadhgc = er33315;
end;

if childyob = 1994 then do;
  if (er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
  else if (er30820 gt 0 and er30820 lt 18) then dadhgc = er30820;
  else if (er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
  else if (er30748 gt 0 and er30748 lt 18) then dadhgc = er30748;
  else if (er33315 gt 0 and er33315 lt 18) then dadhgc = er33315;
  *fill in a few more;
  if (dadhgc = . and er33415 gt 0 and er33415 lt 18) then dadhgc = er33415;
end;

if childyob = 1995 then do;
  if (er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
  else if (er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
  else if (er33315 gt 0 and er33315 lt 18) then dadhgc = er33315;
  else if (er30820 gt 0 and er30820 lt 18) then dadhgc = er30820;
  else if (er33415 gt 0 and er33415 lt 18) then dadhgc = er33415;
  *fill in a few more;
  if (dadhgc = . and er33516 gt 0 and er33516 lt 18) then dadhgc = er33516;
end;

if childyob = 1996 then do;
  if (er33315 gt 0 and er33315 lt 18) then dadhgc = er33315;
  else if (er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
  else if (er33415 gt 0 and er33415 lt 18) then dadhgc = er33415;
  else if (er33115 gt 0 and er33115 lt 18) then dadhgc = er33115;
  else if (er33516 gt 0 and er33516 lt 18) then dadhgc = er33516;
    end;

if childyob = 1997 then do;
  if (er33415 gt 0 and er33415 lt 18) then dadhgc = er33415;
  else if (er33315 gt 0 and er33315 lt 18) then dadhgc = er33315;
  else if (er33516 gt 0 and er33516 lt 18) then dadhgc = er33516;
  else if (er33215 gt 0 and er33215 lt 18) then dadhgc = er33215;
  end;

**for 1997 interview, may use to fill in so many missings;
dhgc97 = .;
if er33415 gt 0 and er33415 lt 18 then dhgc97 = er33415;


proc means;
run;
